package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.AuthorsActivity;
import com.zzk.lanouhn.myapplication1.modles.ThreeSifeSecond;

import java.util.ArrayList;
import java.util.List;

/**
 * 二级页面-->小唐君无图页面（评论 适配器）
 * Created by lanouhn on 16/9/5.
 */
public class ThreeSecondReviewAdapter extends RecyclerView.Adapter<ThreeSecondReviewAdapter.ReviewViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private Context context;
    private List<ThreeSifeSecond.DataBean.PostListBean.CommentsBean> commentsBean = new ArrayList<>();
    private String comments_ID;

    public ThreeSecondReviewAdapter(Context context,
                                    List<ThreeSifeSecond.DataBean.PostListBean.CommentsBean> commentsBean,
                                    String comments_ID) {
        this.context = context;
        this.commentsBean = commentsBean;
        this.comments_ID=comments_ID;
    }

    @Override
    public ReviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.act_sife_second_three_item_review, null);
        ReviewViewHolder holder = new ReviewViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        return holder;
    }

    @Override
    public void onBindViewHolder(ReviewViewHolder holder, final int position) {

        if (commentsBean.size() > 0) {
                holder.lt_view.setVisibility(View.VISIBLE);
            if (position==0){
                holder.ltOne.setVisibility(View.VISIBLE);
                holder.review.setText("查看 全部"+commentsBean.size()+"条评论");
            }else {
                int i=position-1;
                holder.ltTwo.setVisibility(View.VISIBLE);

                holder.author.setText(commentsBean.get(i).getUser().getNickname()+"：");
                holder.rwContent.setText(commentsBean.get(i).getConent());
            }


        }
//
//        holder.lt_view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                Intent intent = new Intent(context, CommentsActivity.class);
//                intent.putExtra("comments_iD", comments_ID);
//                context.startActivity(intent);
//
//            }
//        });
        holder.author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String author_ID =commentsBean.get(position-1).getUser().getUser_id();
                Intent intent = new Intent(context, AuthorsActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return commentsBean.size()+1;
    }


    public class ReviewViewHolder extends RecyclerView.ViewHolder {

        private TextView review, author, rwContent;
        private LinearLayout ltOne, ltTwo,lt_view;

        public ReviewViewHolder(View itemView) {
            super(itemView);

            review= (TextView) itemView.findViewById(R.id.review_three_item_review);
            author = (TextView) itemView.findViewById(R.id.author_three_item_review);
            rwContent = (TextView) itemView.findViewById(R.id.rwContent_three_item_review);
            lt_view= (LinearLayout) itemView.findViewById(R.id.lt_view);
            ltOne= (LinearLayout) itemView.findViewById(R.id.ltOne_review_three_item_review);
            ltTwo= (LinearLayout) itemView.findViewById(R.id.ltTwo_review_three_item_review);
        }
    }
}
